<?php

namespace Mobile\Controller;

/**
 * @description 移动端上传文件中心控制器
 * 
 */
class FileController extends ParentController {

    /**
     * 移动端上传图片公共接口
     * @modify  wenhui 2016-11-28 创建
     */
    public function upload() {
        $param = I('request.');

        $validateRule = array(
            'userId' => 'notnull,notempty,id',
            'config' => 'notnull,notempty',
            'token' => 'notnull,notempty',
        );
        $validator = new \Org\Util\Validate();
        $validatePass = $this->ajaxValidateReturn($validator, $param, $validateRule);
        if (empty($validatePass)) {
                return;
        }

        $userId = $param['userId'];
        $token = $param['token'];
        $config = $param['config'];

        $this->checkMobileToken($userId, $token);

        // 判断是否有上传图片
        $uploadedFile = false;
        if (isset($_FILES['file']['tmp_name'])) {
            $uploadedFile = true;

            // 处理上传图片
            $configInfo = C($config);
            $config = array(
                'maxSize' => $configInfo['maxSize'],
                'rootPath' => $configInfo['rootPath'],
                'savePath' => $configInfo['savePath'],
                'exts' => $configInfo['exts'],
                'subName' => array('date', 'Ymd'),
            );
            $upload = new \Think\Upload($config);
            $info = $upload->upload();
            if (!$info) {
                $this->returnJson(500, 511, $upload->getError());
            }
            $filename = MEIZHU_URL . '/Uploads/' . $info['file']['savepath'] . $info['file']['savename'];
        }
        $imageUrl = $uploadedFile ? $filename : null;

        returnJson(200, 200, array('filename' => $imageUrl));
    }
}
